//
//  YXNameTableView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/25.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXNameTableView.h"
#import "YXUserInfoModel.h"
#import "YXHomeViewModel.h"
#import "YXTradingModel.h"
#define defaultTag 1990
@interface YXNameTableView ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong) YXNameTableViewCell *cell;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@property (nonatomic, retain) NSIndexPath *selectedIndexPath;

@property (nonatomic, assign) NSInteger btnTag;//默认选中的Tag
@end

@implementation YXNameTableView

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)setNameArr:(NSArray *)nameArr {
    _nameArr = nameArr;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.delegate = self;
        self.dataSource = self;
        self.btnTag = defaultTag; //self.btnTag = defaultTag+1  表示默认选择第二个，依次类推
        [self registerClass:[YXNameTableViewCell class] forCellReuseIdentifier:@"YXNameTableViewCell"];
    }
    return self;
}

#pragma mark - UITableView delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YXUserInfoModel *model = self.nameArr[indexPath.row];
    NSString *type;
    if ([model.ID isEqualToString:@"-1"]) {
        type = @"-1";
    }else {
        type = @"0";
    }
    self.nameDidSelectHandlerTwo(model.ID, type);
}




#pragma mark - UITableView data source -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.nameArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID =@"YXNameTableViewCell";
    YXNameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if (!cell) {
        cell = [[YXNameTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellID];
    }
    YXUserInfoModel *model = self.nameArr[indexPath.row];
    cell.tag = defaultTag+indexPath.row;
    cell.nameLab.text = model.name;
    cell.contentView.backgroundColor = color_backColor;
    //设置cell选中时的背景色来定义自己的颜色
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    //设置cell选中时的label颜色
    cell.nameLab.highlightedTextColor = color_blueColor;

    if (indexPath.row==0) {//指定第一行为选中状态
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        self.nameDidSelectHandlerTwo(model.ID, @"-1");
    }

    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}

@end



@implementation YXNameTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.nameLab];
        [self.contentView addSubview:self.lineView];
        [self.contentView addSubview:self.rightLineView];

    }
    return self;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [UILabel setLabelWithText:@"" fontSize:16 color:color_textOne];
        _nameLab.textAlignment = NSTextAlignmentCenter;
       
    }
    return _nameLab;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = color_lineColor;
     
    }
    return _lineView;
}

- (UIView *)rightLineView {
    if (!_rightLineView) {
        _rightLineView = [[UIView alloc] init];
        _rightLineView.backgroundColor = color_lineColor;
  
    }
    return _rightLineView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    YXWeakSelf
    [_nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.contentView.mas_centerY);
        make.centerX.equalTo(weakSelf.contentView.mas_centerX);
        [_nameLab sizeToFit];
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView.mas_left).offset(0);
        make.bottom.equalTo(weakSelf.contentView.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 1));
    }];
    
    [_rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView.mas_right).offset(0);
        make.top.equalTo(weakSelf.contentView.mas_top).offset(0);
        make.size.mas_equalTo(CGSizeMake(1, kScreenHeight));
    }];
}

@end











